package com.acwing.partition11;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class AC1058股票买卖V {

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
        String[] s = reader.readLine().split("\\s+");
        int n = Integer.parseInt(s[0]);
        int[] w = new int[n];
        s = reader.readLine().split("\\s+");
        for (int i = 0; i < n; i++) w[i] = Integer.parseInt(s[i]);
        writer.write(dynamicProgramming(w) + "\n");
        writer.flush();
    }

    private static int dynamicProgramming(int[] w) {
        int[][] dp = new int[w.length + 1][3];
        dp[0][1] = dp[0][0] = -0x3f3f3f3f;
        dp[0][2] = 0;
        for (int i = 1; i <= w.length; i++) {
            dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][2] - w[i - 1]);
            dp[i][1] = dp[i - 1][0] + w[i - 1];
            dp[i][2] = Math.max(dp[i - 1][1], dp[i - 1][2]);
        }
        return Math.max(dp[w.length][1], dp[w.length][2]);
    }
}
